package com.chinawlx.wlxteacher.ui.activity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.WLXRxBus;
import com.chinawlx.wlxteacher.network.bean.WLXHomeworkAddBean;
import com.chinawlx.wlxteacher.ui.event.SendVideoEvent;
import com.chinawlx.wlxteacher.utils.WLXActivityManagerUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;
import com.chinawlx.wlxteacher.wlx_file;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * 作者:吴名
 * 时间:16/8/30
 * 描述:发送视频界面
 */
public class WLXSendVideoActivity extends WLXBaseActivity {
    @BindView(R.id.tv_send)
    TextView mTvSend;
    @BindView(R.id.et_content)
    EditText mEtContent;
    @BindView(R.id.tv_textCount)
    TextView mTvTextCount;
    @BindView(R.id.viedoView)
    VideoView mViedoView;


    private Context mContext;
    private String mContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WLXActivityManagerUtil.addActivity(this);
        mContext = this;
        setContentView(R.layout.activity_send_video);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initViews();
    }

    private void initViews() {
        //显示字数
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mTvTextCount.setText(0 + editable.length() + "/150");
            }
        });
        if (WLXConstant.VIDEO_PATH != "") {
            String videoPath = "file://" + WLXConstant.VIDEO_PATH;
            WLXLogUtil.printE("视频路径:" + videoPath);
            Uri uri = Uri.parse(videoPath);
            mViedoView.setVideoURI(uri);
            mViedoView.setMediaController(new MediaController(mContext));
            mViedoView.requestFocus();
            mViedoView.start();
        }
    }

    public void backMain(View view) {
        onBackPressed();
    }


    @Override
    public void onBackPressed() {
        finish();
    }

    @OnClick({R.id.tv_send, R.id.viedoView})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_send:
                mViedoView.pause();
                mContent = mEtContent.getText().toString();
                addHomeWork();
                break;
            case R.id.viedoView:
                mViedoView.start();
                break;
        }
    }

    /**
     * 告诉服务器要交作业作业
     */
    private void addHomeWork() {
        WLXHttpRxHelper.getInstance().homeworkAddForVideo(WLXConstant.student_id, WLXConstant.grade_id,
                mContent, "video", new Subscriber<WLXHomeworkAddBean>() {


            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(WLXHomeworkAddBean homeworkAddBean) {
                if (homeworkAddBean.getCode() == 0) {
                    WLXConstant.RESOURCE_ID = homeworkAddBean.getData().getId() + "";
                    //存数据库关于视频信息
                    wlx_file file = new wlx_file(WLXConstant.RESOURCE_ID, "homework", 0, 0, "",
                            "waiting", "", System.currentTimeMillis() / 1000);
                    WLXGreenDaoUtil.getFileDao().insert(file);
                    //发送事件到主界面去发送视频
                    WLXRxBus.getInstance().send(new SendVideoEvent());
                    finish();
                } else {
                    WLXCustomToast.show(homeworkAddBean.getMsg());
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        mViedoView.pause();
    }
}
